package com.cy.pj.sys.service.impl;

import com.cy.pj.common.bo.PageObject;
import com.cy.pj.common.exception.ServiceException;
import com.cy.pj.sys.dao.SysUserDao;
import com.cy.pj.sys.service.SysUserService;
import com.cy.pj.sys.vo.SysUserDeptVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author LiuXu
 * @create 2020-07-24 20:28
 */
@Service
public class SysUserServiceImpl implements SysUserService {
    @Autowired
    SysUserDao sysUserDao;

    @Override
    public PageObject<SysUserDeptVo> findPageObjects(String username, Integer pageCurrent) {
        //参数校验
        if (pageCurrent == null || pageCurrent < 1) {
            throw new IllegalArgumentException("当前页码值无效");
        }
        //查询总记录数并校验
        int rowCount = sysUserDao.getRowCount(username);
        if (rowCount == 0) {
            throw new ServiceException("没有找到对应记录");
        }
        //查询当前页记录
        int pageSize = 2;
        int startIndex = (pageCurrent - 1) * pageSize;
        List<SysUserDeptVo> records = sysUserDao.findPageObjects(username, startIndex, pageSize);
        //封装查询结果并返回
        return new PageObject<>(pageCurrent, pageSize, rowCount, records);


    }
}
